﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotSpatial.Symbology;
using System.Drawing;
using DotSpatial.Topology;

namespace HAGISV2
{
    [Serializable]
    public class ConceptShapesStorageAndDisplayStyle
    {
        public ConceptShapesStorageAndDisplayStyle(Color color, double pointSize, DotSpatial.Symbology.PointShape pointShape,double opacity = 1)
        {            
            this._categories = new List<FeatureCategory>();
            this._featureIds = new List<string>();
            this._pointShape = pointShape;
            this._pointSize = pointSize;
            this._color = color;
            this._opacity = (float)opacity;

            ReGenerateFeatureCategories();
        }

        #region 存储

        float _opacity;
        /// <summary>
        /// 透明度
        /// </summary>
        public float Opacity
        {
            get { return _opacity; }
            set 
            {
                _opacity = value;
                if (this._categories.Count == 3)
                    ((this._categories[2].Symbolizer as PolygonSymbolizer).Patterns[0] as SimplePattern).Opacity = value;
            }
        }

        DotSpatial.Symbology.PointShape _pointShape;
        /// <summary>
        /// 点源形状
        /// </summary>
        public DotSpatial.Symbology.PointShape PointShape
        {
            get { return _pointShape; }
            set
            {
                _pointShape = value;
                if (this._categories.Count == 3)
                    ((this._categories[0].Symbolizer as PointSymbolizer).Symbols[0] as SimpleSymbol).PointShape= value;
            }
        }

        double _pointSize;
        /// <summary>
        /// 点状源汇形状大小
        /// </summary>
        public double PointSize
        {
            get { return _pointSize; }
            set
            {
                _pointSize = value;
                if (this._categories.Count == 3)
                    ((this._categories[0].Symbolizer as PointSymbolizer).Symbols[0] as SimpleSymbol).Size = new Size2D(value, value);
            }
        }

        double _lineWidth = 1;
        /// <summary>
        /// 线或多边形边框宽度 默认为1
        /// </summary>
        public double LineWidth
        {
            get { return _lineWidth; }
            set
            {
                _lineWidth = value;
                ((this._categories[1].Symbolizer as LineSymbolizer).Strokes[0] as SimpleStroke).Width = value;
            }
        }

        Color _color;
        /// <summary>
        /// 颜色
        /// </summary>
        public Color Color
        {
            get { return _color; }
            set
            {
                _color = value;
                if (this._categories.Count == 3)
                {
                    ((this._categories[0].Symbolizer as PointSymbolizer).Symbols[0] as SimpleSymbol).Color = value;

                    ((this._categories[1].Symbolizer as LineSymbolizer).Strokes[0] as SimpleStroke).Color = value;

                    (((this._categories[2].Symbolizer as PolygonSymbolizer).Patterns[0] as SimplePattern).Outline.Strokes[0] as SimpleStroke).Color = value;
                    ((this._categories[2].Symbolizer as PolygonSymbolizer).Patterns[0] as SimplePattern).FillColor = value;
                    ((this._categories[2].Symbolizer as PolygonSymbolizer).Patterns[0] as SimplePattern).Opacity = this._opacity;
                }
            }
        }

        [NonSerialized]
        List<FeatureCategory> _categories;
        /// <summary>
        /// feature（源汇项）表现样式
        /// </summary>
        public List<FeatureCategory> Categories
        {
            get { return _categories; }
        }

        List<string> _featureIds;
        /// <summary>
        /// 存储属于该样式的Feature(源汇项)的Id
        /// </summary>
        public List<string> FeatureIds
        {
            get { return _featureIds; }
        }

        #endregion


        #region 方法

        /// <summary>
        /// 添加新的Feature Id
        /// </summary>
        /// <param name="featureid"></param>
        public void AddFeatureId(string featureid)
        {
            if (!this._featureIds.Contains(featureid))
                this._featureIds.Add(featureid);
        }

        /// <summary>
        /// 添加新的Feature Ids
        /// </summary>
        /// <param name="featureids"></param>
        public void AddFeatureId(List<string> featureids)
        {
            foreach (string item in featureids)
            {
                this.AddFeatureId(item);
            }
        }

        /// <summary>
        /// 删除该样式中的指定Featureid
        /// </summary>
        /// <param name="featureId"></param>
        public void RemoveFeatureId(string featureId)
        {
            if (this._featureIds.Contains(featureId))
                this._featureIds.Remove(featureId);
        }

        /// <summary>
        /// 删除该样式中的指定Feature ids
        /// </summary>
        /// <param name="featureIds"></param>
        public void RemoveFeatureId(List<string> featureIds)
        {
            foreach (string item in featureIds)
            {
                this.RemoveFeatureId(featureIds);
            }
        }

        /// <summary>
        /// 获取表现样式
        /// </summary>
        public FeatureCategory GetFeatureCategory(MFSHAPETYPE type)
        {
            if (_categories == null || _categories.Count != 3)
            {
                ReGenerateFeatureCategories();
            }
            switch (type)
            {
                case MFSHAPETYPE.POINT:
                    return _categories[0];
                case MFSHAPETYPE.LINE:
                    return _categories[1];
                case MFSHAPETYPE.POLYGON:
                    return _categories[2];
                default:
                    return null;
            }
        }

        #endregion

        #region 内部方法

        void ReGenerateFeatureCategories()
        {
            if (_categories == null)
                _categories = new List<FeatureCategory>();
            else
                this._categories.Clear();

            this._categories.Add(new PointCategory(this._color, this._pointShape, this._pointSize));
            if (_pointShape == DotSpatial.Symbology.PointShape.Triangle)
                (this._categories[0] as PointCategory).Symbolizer.Symbols[0].Angle = 30;

            this._categories.Add(new LineCategory(this._color, this._lineWidth));

            this._categories.Add(new PolygonCategory(this._color,this._color,  this._lineWidth));
            ((this._categories[2].Symbolizer as PolygonSymbolizer).Patterns[0] as SimplePattern).Opacity = this._opacity;
        }


        #endregion
    }
}
